//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    memberData: {}
  },
  onShow() {
    let _this = this
    wx.getStorage({
      key: 'indexMemberData',
      success(res) {
        _this.setData({
          memberData: JSON.parse(res.data)
        })
      }
    })
  },
  changeInput(e) {
    if (e.target.id == 'gender' || e.target.id == 'calendar') {
      this.data.memberData[e.target.id] = Number(e.detail.value)
    } else {
      this.data.memberData[e.target.id] = e.detail.value
    }
    this.setData({
      memberData: this.data.memberData
    })
  },
  updateMember() {
    app.$http('/members/' + this.data.memberData.id, this.data.memberData, 'put').then(response => {
      wx.showToast({
        title: '修改成功',
        icon: 'success',
        duration: 2000
      })
      setTimeout(_ => {
        wx.navigateBack({
          delta: 1
        })
      }, 2000)
    }, error => {})
  },
  removeMembder() {
    let _this = this
    wx.showModal({
      title: '删除确认',
      content: '删除后不可恢复，请谨慎操作',
      success(res) {
        if (res.confirm) {
          app.$http('/members/' + _this.data.memberData.id, {}, 'DELETE').then(response => {
            wx.showToast({
              title: '删除成功',
              icon: 'success',
              duration: 2000
            })
            setTimeout(_ => {
              wx.navigateBack({
                delta: 1
              })
            }, 2000)
          }, error => {})
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
})
